package com.example.empleemanagesystem.service.impl;

import com.example.empleemanagesystem.entity.SalaryManage;
import com.example.empleemanagesystem.repository.SalaryManageRepository;
import com.example.empleemanagesystem.service.SalaryManageService;
import com.example.empleemanagesystem.vo.SalaryManageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

// 标记这个类是一个服务层组件
@Service
public class SalaryManageServiceImpl implements SalaryManageService {
    @Autowired // 自动注入SalaryManageRepository实例
    private SalaryManageRepository salaryManageRepository;

    @Override
    public List<SalaryManageVO> findAll() {
        // 获取所有薪资记录，并将其转换为SalaryManageVO对象列表
        return salaryManageRepository.findAll().stream().map(salaryManage -> {
            SalaryManageVO salaryManageVO = new SalaryManageVO();
            salaryManageVO.setId(salaryManage.getId());
            salaryManageVO.setNumber(salaryManage.getNumber());
            salaryManageVO.setName(salaryManage.getName());
            salaryManageVO.setBaseSalary(salaryManage.getBaseSalary());
            salaryManageVO.setBonus(salaryManage.getBonus());
            salaryManageVO.setDeduction(salaryManage.getDeduction());
            return salaryManageVO;
        }).collect(Collectors.toList());//对流中的元素进行最终的收集汇总，转换为一个列表
    }

    @Override
    public Optional<SalaryManage> getByName(@NonNull String name) {
        // 根据用户名获取薪资记录列表，并返回第一个匹配的记录
        List<SalaryManage> result = salaryManageRepository.getSalaryByName(name);
        return Optional.ofNullable(result.isEmpty() ? null : result.get(0));
    }

    @Override
    public Optional<SalaryManage> getByNumber(@NonNull String number) {
        // 根据员工编号获取薪资记录列表，并返回第一个匹配的记录
        List<SalaryManage> result = salaryManageRepository.getSalaryByNumber(number);
        return Optional.ofNullable(result.isEmpty() ? null : result.get(0));
    }

    @Override
    public boolean create(@NonNull SalaryManageVO salaryManageVO) {
        // 创建新的薪资记录
        SalaryManage salaryManage = new SalaryManage();
        salaryManage.setNumber(salaryManageVO.getNumber());
        salaryManage.setName(salaryManageVO.getName());
        salaryManage.setBaseSalary(salaryManageVO.getBaseSalary());
        salaryManage.setBonus(salaryManageVO.getBonus());
        salaryManage.setDeduction(salaryManageVO.getDeduction());
        try {
            // 保存薪资记录到数据库
            salaryManage = salaryManageRepository.save(salaryManage);
            salaryManageVO.setId(salaryManage.getId());
            return true;
        } catch (Exception e) {
            System.err.println("创建薪资记录出错！");
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public SalaryManage getById(@NonNull Long id) {
        // 根据ID获取薪资记录
        return salaryManageRepository.findById(id).orElse(null);
    }

    @Override
    public void deleteById(Long id) {
        try {
            // 根据ID删除薪资记录
            salaryManageRepository.deleteById(id);
        } catch (Exception e) {
            System.err.println("删除薪资记录出错！");
            e.printStackTrace();
        }
    }

    @Override
    public void update(SalaryManage salaryManage) {
        try {
            // 更新薪资记录
            salaryManageRepository.save(salaryManage);
        } catch (Exception e) {
            System.err.println("更新薪资记录出错！");
            e.printStackTrace();
        }
    }
}